package com.clw.classpath;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/**
 * Created by chengliwei on 2018/12/14.
 */
public class ZipEntry implements Entry {

    private String absPath;

    public ZipEntry(String absPath) {
        this.absPath = absPath;
    }

    @Override
    public byte[] readClass(String className) throws IOException {
        String filePath = absPath;
        ZipFile zipFile = new ZipFile(filePath);
        InputStream in = new BufferedInputStream(new FileInputStream(filePath));
        ZipInputStream zin = new ZipInputStream(in);
        java.util.zip.ZipEntry zipEntry;
        while ((zipEntry = zin.getNextEntry()) != null) {
            if (!zipEntry.isDirectory()
                    && zipEntry.getName().equals(className)
                    && zipEntry.getSize() > 0) {
                InputStream zipIn = zipFile.getInputStream(zipEntry);
                byte[] content = new byte[zipIn.available()];
                zipIn.read(content);
                return content;
            }
        }
        return null;
    }
}
